package com.foreknow.web;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.foreknow.entity.ConstantType;
import com.foreknow.service.ConstantTypeService;
import com.foreknow.utils.Constance;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Api(tags = "ConstantTypeController", description = "常数分类操作")
@RestController
@RequestMapping("/constanttypecontroller")
public class ConstantTypeController {
    @Autowired
    private ConstantTypeService constantTypeService;

    @ApiOperation("获取常数分类列表")
    @RequestMapping(value = "/getconstanttypelist",method = RequestMethod.POST)
    public  String getConstantTypeList(@RequestBody Map<String,Object> para) throws JsonProcessingException {
        String query = (String) para.get("query");
        Integer pagenum = (Integer) para.get("pagenum");
        Integer pagesize = (Integer) para.get("pagesize");
        ConstantType ct = new ConstantType();
        ct.setConstanttypecode(query);
        ct.setConstanttypename(query);
        //设置分页信息
        PageHelper.startPage(pagenum,pagesize);
        List<ConstantType>list = constantTypeService.getConstantTypeList(ct);
        PageInfo<ConstantType>info = new PageInfo<>(list);
        HashMap<String,Object> hs=new HashMap<>();
        hs.put("totalpage",info.getTotal());
        hs.put("pagenum",info.getPageNum());
        hs.put("data",list);
        if(list!=null){

            hs.put("meta",new Constance("获取成功",200));
        }else {
            hs.put("meta",new Constance("没有获取到数据",403));
        }

        ObjectMapper objectMapper = new ObjectMapper();
        return objectMapper.writeValueAsString(hs);
    }

    @ApiOperation("添加常数类别")
    @RequestMapping(value = "/insert",method = RequestMethod.POST)
    public  String insert(@RequestBody ConstantType constantType) throws JsonProcessingException {

        int i = constantTypeService.insert(constantType);
        HashMap<String,Object> hs=new HashMap<>();
        if(i==1){
            hs.put("data",1);
            hs.put("meta",new Constance("msg",200));
        }else {
            hs.put("meta",new Constance("msg",403));
        }

        ObjectMapper objectMapper = new ObjectMapper();
        return objectMapper.writeValueAsString(hs);
    }

    @ApiOperation("删除常数类别")
    @RequestMapping(value = "/delete" ,method = RequestMethod.PUT)
    public  String deleteById(@RequestBody ConstantType constantType) throws JsonProcessingException {
        int a = constantType.getId();
        int i = constantTypeService.delete(constantType.getId());
        HashMap<String,Object> hs=new HashMap<>();
        if(i==1) {
            hs.put("meta",new Constance("msg",200));
        }else {
            hs.put("meta",new Constance("msg",403));
        }
        ObjectMapper objectMapper = new ObjectMapper();
        return objectMapper.writeValueAsString(hs);
    }

    @ApiOperation("通过id查询")
    @RequestMapping(value = "/getconstanttype",method = RequestMethod.GET)
    public String selectByPrimaryKey(@RequestParam("id") Integer id) throws JsonProcessingException {
        int a = id;
        ConstantType ct = constantTypeService.selectByPrimaryKey(a);
        HashMap<String,Object> hs = new HashMap<>();
        if(ct!=null){
            hs.put("data",ct);
            hs.put("meta",new Constance("msg",200));
        }else {
            hs.put("meta",new Constance("msg",403));
        }
        ObjectMapper objectMapper = new ObjectMapper();
        return objectMapper.writeValueAsString(hs);
    }

    @ApiOperation("通过id修改")
    @RequestMapping(value = "/update",method = RequestMethod.PUT)
    public String selectByPrimaryKey(@RequestBody ConstantType constantType) throws JsonProcessingException {
        int  a = constantTypeService.updateByPrimaryKeySelective(constantType);
        HashMap<String,Object> hs = new HashMap<>();
        if(a==1){
            hs.put("data",1);
            hs.put("meta",new Constance("msg",200));
        }else {
            hs.put("meta",new Constance("msg",403));
        }

        ObjectMapper objectMapper = new ObjectMapper();
        return objectMapper.writeValueAsString(hs);

    }

    @ApiOperation("获取下拉列表")
    @RequestMapping(value = "/getlist",method = RequestMethod.GET)
    public String getConstantType() throws JsonProcessingException {
        List<ConstantType>list = constantTypeService.getConstantType();
        HashMap<String,Object> hs=new HashMap<>();
        if(list!=null){
            hs.put("data",list);
            hs.put("meta",new Constance("msg",200));
        }else {
            hs.put("meta",new Constance("msg",403));
        }

        ObjectMapper objectMapper = new ObjectMapper();
        return objectMapper.writeValueAsString(hs);
    }

    @ApiOperation("批量删除常数类别")
    @RequestMapping(value = "/deleteall" ,method = RequestMethod.PUT)
    public  String deleteAll (@RequestBody Map<String,Object> para) throws JsonProcessingException {
        List<Integer> listAll = (List<Integer>)para.get("list");
        List<ConstantType>list = new ArrayList<>();
        HashMap<String,Object> hs=new HashMap<>();
        if(listAll!=null){
            for (Integer id: listAll) {
                int result =  constantTypeService.delete(id);
                if(result!=1){
                    ConstantType ct = constantTypeService.selectByPrimaryKey(id);
                    list.add(ct);
                }
            }
            if(list.size()==0) {
                hs.put("meta",new Constance("删除成功",200));
            }else {
                hs.put("meta",new Constance("有部分删除失败",403));
                hs.put("faild",list);
            }
        }else {
            hs.put("meta",new Constance("删除失败",403));
        }

        ObjectMapper objectMapper = new ObjectMapper();
        return objectMapper.writeValueAsString(hs);
    }
}
